package com.fable.web.page;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromContextPath;

/**
 * Velocity模板路径及文件名定义
 */
public final class P {

    private P() {

    }

    /**
     * 功能链接
     */
    public enum UrlEnum {

        //客户
        customerList("客户搜索", "customer/customerList.htm"),
        addCustomer("新增客户页面", "customer/addCustomer.htm"),
        saveCustomer("新增客户保存", "customer/editCustomer.json"),
        editCustomer("编辑客户页面", "customer/editCustomer.htm"),
        editSaveCustomer("编辑客户保存", "customer/editCustomer.json"),
        deleteCustomer("删除客户", "delCustomer.json"),
        exportCustomer("导出客户", "customer/export.htm"),

        //关键词
        keywordList("关键词列表", "keyword/keywordList.htm"),
        saveKeyword("关键词列表", "keyword/saveKeyword.json"),
        delKeyword("删除关键词", "keyword/delKeyword.json"),

        //日报
        workdailyList("日报搜索", "workdaily/workdailyList.htm"),
        addWorkdaily("新增日报", "customer/addWorkdaily.htm"),
        saveWorkdaily("新增日报保存", "customer/editWorkdaily.json"),
        editWorkdaily("修改日报", "customer/editWorkdaily.htm"),

        expect("敬请期待", "customer/expect"),
        login("登陆页面", "/login.htm"),
        loginJson("登陆", "/login.json"),
        loginOut("退出", "/loginOut.htm"),;

        private String name;
        private String value;

        UrlEnum(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return value;
        }

        public void setUrl(String url) {
            this.value = url;
        }

    }


    public static final class Common {
        public static final String login = "login";
        public static final String error = "/error";
        public static final String center = "/center";
        public static final String welcome = "/welcome";
        /**
         * 公海客户
         */
        public static final String pubCustomerList = "customer/pubCustomerList";
        public static final String pubCustomerAdd = "customer/pubCustomerAdd";


        /**
         * 私海客户
         */
        public static final String priCustomerList = "customer/priCustomerList";
        public static final String priCustomerAdd = "customer/priCustomerAdd";

        /**
         * 服务中客户
         */
        public static final String serCustomerList = "customer/serCustomerList";


        /**
         * 客户详情
         */
        public static final String customerDetail = "customer/customerDetail";

        /**
         * 客户类型
         */
        public static final String customerTypeList = "customer/customerTypeList";
        public static final String addCustomerType = "customer/addCustomerType";

        /**
         * 客户来源
         */
        public static final String customerSourceList = "customer/customerSourceList";
        public static final String addCustomerSource = "customer/addCustomerSource";

        /**
         * 客户小计
         */
        public static final String contactsList = "customer/contactsList";
        public static final String contactsAdd = "customer/contacksAdd";


        public static final ModelAndView error(String errorMsg) {
            return new ModelAndView(error, "msg", errorMsg);
        }
    }

    /**
     * 内部跳转路径
     */
    public static final class RedirectUrl {
        public static final String login = "/login";
        public static final String error = "/error";
        public static final String center = "/center";

    }

    /**
     * 内部controller跳转:
     * <p/>
     * 参数：mv.addObject("id", param.getId());
     * <p/>
     * 示例：url=/module/toPage.htm
     * <p/>
     * 结果：跳转到url?id='id'
     *
     * @param url
     * @return
     */
    public static final String redirectPrex(String url) {
        return "redirect:" + url;
    }

    /**
     * 全路径的URL方式:可跳转到任意URL
     * <p/>
     * 参数：mv.addObject("id", param.getId());
     * <p/>
     * 示例：url = http://localhost:8280/……/toPage.htm
     * <p/>
     * 结果：http://localhost:8280/……/toPage?id='id'
     *
     * @param url
     * @return
     */
    public static final RedirectView redirectTo(String url) {
        return new RedirectView(url);
    }

    /**
     * 全路径的URL方式
     */
    public static final class RedirectTo {
        public static final RedirectView index(HttpServletRequest request) {
            return redirectTo(fromContextPath(request).path("/index.htm").build().toString());
        }

        public static final RedirectView login(HttpServletRequest request) {
            return redirectTo(fromContextPath(request).path("/login.htm").build().toString());
        }
    }
}
